package com.example.conf;

import com.example.pojo.RedisField;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class RedisJsonConf {
    /**
     * 加载数据 JSON 配置
     * @param configFilePath
     * @return RedisField 对象列表
     * @throws Exception
     */
    public List<RedisField> loadJSON(String configFilePath) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(new File(configFilePath), objectMapper.getTypeFactory().constructCollectionType(List.class, RedisField.class));
    }

    /**
     * 将redis中的数据转换为可读性数据
     * @param RedisData
     * @param JSONPath
     * @return 转换后的可读性数据
     * @throws Exception
     */
    public Map<String, String> processData_Get(Map<Object, Object> RedisData, String JSONPath) throws Exception {
        // 加载配置
        List<RedisField> fields = loadJSON(JSONPath);

        Map<String, String> result = new HashMap<>();
        // 遍历每个字段
        for (RedisField field : fields) {
            // 从 Redis 获取值
            //String value = getValueFromRedis(field.getRedisKey());
            String value = RedisData.get(field.getRedisKey()).toString();

            // 根据 valid_values 转换值
            if (field.getValidValues() != null && !field.getValidValues().isEmpty()) {
                String displayValue = field.getValidValues().getOrDefault(value, "未知值: " + value);
                //System.out.println(field.getName() + ": " + displayValue);
                result.put(field.getName(), displayValue);
            } else {
                // 如果没有 valid_values，则直接输出原始值
                //System.out.println(field.getName() + ": " + value);
                result.put(field.getName(), value);
            }
        }
        return result;
    }

    /**
     * 将可读性数据转换成结构化数据并写入Redis
     * @param RequestData
     * @param JSONPath
     * @return Redis结构化数据
     * @throws Exception
     */
    public Map<String, Object> processData_Set(Map<String, String> RequestData, String JSONPath) throws Exception {
        // 加载配置
        List<RedisField> fields = loadJSON(JSONPath);

        Map<String, Object> result = new HashMap<>();
        Boolean isContainRedisKey = true;
        Boolean isContainRedisValue = true;
        for(String name : RequestData.keySet()){
            // 使用 stream 筛选 redisKey == "ROV" 的对象
            Optional<RedisField> Field = fields.stream()
                    .filter(field -> name.equals(field.getName()))
                    .findFirst();

            String Redis_Key = "", Redis_Value = "";
            // 判断是否存在名称为name的RedisKey
            if (Field.isPresent()) {
                RedisField field = Field.get();
                Redis_Key = field.getRedisKey();

                /*存在valid_value*/
                if (field.getValidValues() != null && !field.getValidValues().isEmpty()){
                    Map<String, String> validValues = field.getValidValues();
                    Boolean flag = false;
                    for (Map.Entry<String, String> entry : validValues.entrySet()) {
                        // 判断是否存在值为传入的数据的RedisValue
                        if(entry.getValue().equals(RequestData.get(name))){
                            Redis_Value = entry.getKey();
                            flag = true;
                        }
                    }
                    if(!flag) {
                        System.out.println("未找到 value 为 "+RequestData.get(name)+" 的valid_value");
                        isContainRedisValue = false;
                    }
                }
                else{
                    // 如果没有 valid_values，则直接存入原始值
                    Redis_Value = RequestData.get(name);
                }
                System.out.println("找到 name 为 "+name+" 的RedisKey "+ Redis_Key);
                result.put(Redis_Key, Redis_Value);
            } else {
                System.out.println("未找到 name 为 "+name+" 的RedisKey");
                isContainRedisKey = false;
            }
        }
        result.put("isContainRedisKey", isContainRedisKey);
        result.put("isContainRedisValue", isContainRedisValue);
        return result;
    }
}

